diff --git a/cpp/c_api.cc b/cpp/c_api.cc index 83d0180..ae9eac5 100644 --- a/cpp/c_api.cc +++ b/cpp/c_api.cc @@ -53,8 +53,8 @@ thread_local Any last_error; } // namespace MLC_API MLCAny MLCGetLastError() { - MLCAny ret; - static_cast(ret) = std::move(last_error); + MLCAny ret = static_cast(last_error); + static_cast(last_error) = MLCAny(); return ret; } diff --git a/include/mlc/base/lib.h b/include/mlc/base/lib.h index d31ee51..01729f8 100644 --- a/include/mlc/base/lib.h +++ b/include/mlc/base/lib.h @@ -13,7 +13,7 @@ struct VTable { this->Swap(other); return *this; } - ~VTable() { MLC_CHECK_ERR(::MLCVTableDelete(self), nullptr); } + ~VTable() { MLC_CHECK_ERR(::MLCVTableDelete(self)); } template R operator()(Args... args) const; template VTable &Set(Func func); @@ -41,17 +41,17 @@ struct Lib { static FuncObj *_init(int32_t type_index) { return VTableGetFunc(init, type_index, "__init__"); } static VTable MakeVTable(const char *name) { MLCVTableHandle vtable = nullptr; - MLC_CHECK_ERR(::MLCVTableCreate(_lib, name, &vtable), nullptr); + MLC_CHECK_ERR(::MLCVTableCreate(_lib, name, &vtable)); return VTable(vtable); } MLC_INLINE static MLCTypeInfo *GetTypeInfo(int32_t type_index) { MLCTypeInfo *type_info = nullptr; - MLC_CHECK_ERR(::MLCTypeIndex2Info(_lib, type_index, &type_info), nullptr); + MLC_CHECK_ERR(::MLCTypeIndex2Info(_lib, type_index, &type_info)); return type_info; } MLC_INLINE static MLCTypeInfo *GetTypeInfo(const char *type_key) { MLCTypeInfo *type_info = nullptr; - MLC_CHECK_ERR(::MLCTypeKey2Info(_lib, type_key, &type_info), nullptr); + MLC_CHECK_ERR(::MLCTypeKey2Info(_lib, type_key, &type_info)); return type_info; } MLC_INLINE static const char *GetTypeKey(int32_t type_index) { @@ -77,14 +77,14 @@ struct Lib { } MLC_INLINE static MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key) { MLCTypeInfo *info = nullptr; - MLC_CHECK_ERR(::MLCTypeRegister(_lib, parent_type_index, type_key, type_index, &info), nullptr); + MLC_CHECK_ERR(::MLCTypeRegister(_lib, parent_type_index, type_key, type_index, &info)); return info; } private: static FuncObj *VTableGetFunc(MLCVTableHandle vtable, int32_t type_index, const char *vtable_name) { MLCAny func{}; - MLC_CHECK_ERR(::MLCVTableGetFunc(vtable, type_index, true, &func), &func); + MLC_CHECK_ERR(::MLCVTableGetFunc(vtable, type_index, true, &func)); if (!::mlc::base::IsTypeIndexPOD(func.type_index)) { ::mlc::base::DecRef(func.v.v_obj); } @@ -100,12 +100,12 @@ struct Lib { } static MLCVTableHandle VTableGetGlobal(const char *name) { MLCVTableHandle ret = nullptr; - MLC_CHECK_ERR(::MLCVTableGetGlobal(_lib, name, &ret), nullptr); + MLC_CHECK_ERR(::MLCVTableGetGlobal(_lib, name, &ret)); return ret; } static MLC_SYMBOL_HIDE inline MLCTypeTableHandle _lib = []() { MLCTypeTableHandle ret = nullptr; - MLC_CHECK_ERR(::MLCHandleGetGlobal(&ret), nullptr); + MLC_CHECK_ERR(::MLCHandleGetGlobal(&ret)); return ret; }(); static MLC_SYMBOL_HIDE inline MLCVTableHandle cxx_str = VTableGetGlobal("__cxx_str__"); diff --git a/include/mlc/base/utils.h b/include/mlc/base/utils.h index a74d784..a6658ed 100644 --- a/include/mlc/base/utils.h +++ b/include/mlc/base/utils.h @@ -77,9 +77,9 @@ } \ MLC_UNREACHABLE() -#define MLC_CHECK_ERR(Call, Ret) \ +#define MLC_CHECK_ERR(Call) \ if (int32_t err_code = (Call)) { \ - ::mlc::base::FuncCallCheckError(err_code, (Ret)); \ + ::mlc::base::FuncCallCheckError(err_code, nullptr); \ } namespace mlc { diff --git a/include/mlc/core/all.h b/include/mlc/core/all.h index 7a6310e..6145e2c 100644 --- a/include/mlc/core/all.h +++ b/include/mlc/core/all.h @@ -147,12 +147,12 @@ inline Any Lib::IRPrint(AnyView obj, AnyView printer, AnyView path) { return ret; } inline int32_t Lib::FuncSetGlobal(const char *name, FuncObj *func, bool allow_override) { - MLC_CHECK_ERR(::MLCFuncSetGlobal(_lib, name, Any(func), allow_override), nullptr); + MLC_CHECK_ERR(::MLCFuncSetGlobal(_lib, name, Any(func), allow_override)); return 0; } inline FuncObj *Lib::FuncGetGlobal(const char *name, bool allow_missing) { Any ret; - MLC_CHECK_ERR(::MLCFuncGetGlobal(_lib, name, &ret), &ret); + MLC_CHECK_ERR(::MLCFuncGetGlobal(_lib, name, &ret)); if (!ret.defined() && !allow_missing) { MLC_THROW(KeyError) << "Missing global function: " << name; } @@ -205,12 +205,12 @@ template inline R VTable::operator()(Args... args AnyViewArray stack_args; Any ret; stack_args.Fill(std::forward(args)...); - MLC_CHECK_ERR(::MLCVTableCall(self, N, stack_args.v, &ret), &ret); + MLC_CHECK_ERR(::MLCVTableCall(self, N, stack_args.v, &ret)); + return ret; } template inline VTable &VTable::Set(Func func) { constexpr bool override_mode = false; - int32_t type_index = Obj::_type_index; - MLC_CHECK_ERR(::MLCVTableSetFunc(this->self, type_index, func.get(), override_mode), nullptr); + MLC_CHECK_ERR(::MLCVTableSetFunc(this->self, Obj::_type_index, func.get(), override_mode)); return *this; } diff --git a/include/mlc/core/func.h b/include/mlc/core/func.h index 701e18d..8e009f7 100644 --- a/include/mlc/core/func.h +++ b/include/mlc/core/func.h @@ -104,8 +104,8 @@ inline void FuncCall(const void *self, int32_t num_args, const MLCAny *args, MLC const MLCFunc *func = static_cast(self); if (func->call && reinterpret_cast(func->safe_call) == reinterpret_cast(FuncObj::SafeCallImpl)) { func->call(func, num_args, args, ret); - } else { - MLC_CHECK_ERR(func->safe_call(func, num_args, args, ret), ret); + } else if (int32_t err_code = func->safe_call(func, num_args, args, ret)) { + FuncCallCheckError(err_code, ret); } } template inline auto GetGlobalFuncCall(const char *name) { diff --git a/include/mlc/core/func_details.h b/include/mlc/core/func_details.h index 88e591c..71cafa2 100644 --- a/include/mlc/core/func_details.h +++ b/include/mlc/core/func_details.h @@ -190,12 +190,16 @@ template MLC_INLINE FuncObj *FuncObj::Allocator::N inline Ref FuncObj::FromForeign(void *self, MLCDeleterType deleter, MLCFuncSafeCallType safe_call) { if (deleter == nullptr) { return Ref::New([self, safe_call](int32_t num_args, const MLCAny *args, MLCAny *ret) { - MLC_CHECK_ERR(safe_call(self, num_args, args, ret), ret); + if (int32_t err_code = safe_call(self, num_args, args, ret)) { + ::mlc::base::FuncCallCheckError(err_code, ret); + } }); } else { return Ref::New( [self = std::shared_ptr(self, deleter), safe_call](int32_t num_args, const MLCAny *args, MLCAny *ret) { - MLC_CHECK_ERR(safe_call(self.get(), num_args, args, ret), ret); + if (int32_t err_code = safe_call(self.get(), num_args, args, ret)) { + ::mlc::base::FuncCallCheckError(err_code, ret); + } }); } } diff --git a/include/mlc/core/reflection.h b/include/mlc/core/reflection.h index ea566e0..f0c4f6a 100644 --- a/include/mlc/core/reflection.h +++ b/include/mlc/core/reflection.h @@ -100,14 +100,12 @@ struct _Reflect { reinterpret_cast(func_any_to_ref.v.v_obj), // kStaticFn}); } - MLC_CHECK_ERR(::MLCTypeRegisterFields(nullptr, this->type_index, this->fields.size(), this->fields.data()), - nullptr); + MLC_CHECK_ERR(::MLCTypeRegisterFields(nullptr, this->type_index, this->fields.size(), this->fields.data())); MLC_CHECK_ERR(::MLCTypeRegisterStructure(nullptr, this->type_index, static_cast(this->structure_kind), this->sub_structure_indices.size(), this->sub_structure_indices.data(), - this->sub_structure_kinds.data()), - nullptr); + this->sub_structure_kinds.data())); for (const MLCTypeMethod &method : this->methods) { - MLC_CHECK_ERR(::MLCTypeAddMethod(nullptr, this->type_index, method), nullptr); + MLC_CHECK_ERR(::MLCTypeAddMethod(nullptr, this->type_index, method)); } } return 0;