Skip to content

Commit 4f414eb

Browse files
authored
fix(core): Correct Error Checking (#22)
1 parent cd145d9 commit 4f414eb

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

cpp/c_api.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ thread_local Any last_error;
5353
} // namespace
5454

5555
MLC_API MLCAny MLCGetLastError() {
56-
MLCAny ret;
57-
static_cast<Any &>(ret) = std::move(last_error);
56+
MLCAny ret = static_cast<MLCAny &>(last_error);
57+
static_cast<MLCAny &>(last_error) = MLCAny();
5858
return ret;
5959
}
6060

include/mlc/base/lib.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct VTable {
1313
this->Swap(other);
1414
return *this;
1515
}
16-
~VTable() { MLC_CHECK_ERR(::MLCVTableDelete(self), nullptr); }
16+
~VTable() { MLC_CHECK_ERR(::MLCVTableDelete(self)); }
1717

1818
template <typename R, typename... Args> R operator()(Args... args) const;
1919
template <typename Obj> VTable &Set(Func func);
@@ -41,17 +41,17 @@ struct Lib {
4141
static FuncObj *_init(int32_t type_index) { return VTableGetFunc(init, type_index, "__init__"); }
4242
static VTable MakeVTable(const char *name) {
4343
MLCVTableHandle vtable = nullptr;
44-
MLC_CHECK_ERR(::MLCVTableCreate(_lib, name, &vtable), nullptr);
44+
MLC_CHECK_ERR(::MLCVTableCreate(_lib, name, &vtable));
4545
return VTable(vtable);
4646
}
4747
MLC_INLINE static MLCTypeInfo *GetTypeInfo(int32_t type_index) {
4848
MLCTypeInfo *type_info = nullptr;
49-
MLC_CHECK_ERR(::MLCTypeIndex2Info(_lib, type_index, &type_info), nullptr);
49+
MLC_CHECK_ERR(::MLCTypeIndex2Info(_lib, type_index, &type_info));
5050
return type_info;
5151
}
5252
MLC_INLINE static MLCTypeInfo *GetTypeInfo(const char *type_key) {
5353
MLCTypeInfo *type_info = nullptr;
54-
MLC_CHECK_ERR(::MLCTypeKey2Info(_lib, type_key, &type_info), nullptr);
54+
MLC_CHECK_ERR(::MLCTypeKey2Info(_lib, type_key, &type_info));
5555
return type_info;
5656
}
5757
MLC_INLINE static const char *GetTypeKey(int32_t type_index) {
@@ -77,14 +77,14 @@ struct Lib {
7777
}
7878
MLC_INLINE static MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key) {
7979
MLCTypeInfo *info = nullptr;
80-
MLC_CHECK_ERR(::MLCTypeRegister(_lib, parent_type_index, type_key, type_index, &info), nullptr);
80+
MLC_CHECK_ERR(::MLCTypeRegister(_lib, parent_type_index, type_key, type_index, &info));
8181
return info;
8282
}
8383

8484
private:
8585
static FuncObj *VTableGetFunc(MLCVTableHandle vtable, int32_t type_index, const char *vtable_name) {
8686
MLCAny func{};
87-
MLC_CHECK_ERR(::MLCVTableGetFunc(vtable, type_index, true, &func), &func);
87+
MLC_CHECK_ERR(::MLCVTableGetFunc(vtable, type_index, true, &func));
8888
if (!::mlc::base::IsTypeIndexPOD(func.type_index)) {
8989
::mlc::base::DecRef(func.v.v_obj);
9090
}
@@ -100,12 +100,12 @@ struct Lib {
100100
}
101101
static MLCVTableHandle VTableGetGlobal(const char *name) {
102102
MLCVTableHandle ret = nullptr;
103-
MLC_CHECK_ERR(::MLCVTableGetGlobal(_lib, name, &ret), nullptr);
103+
MLC_CHECK_ERR(::MLCVTableGetGlobal(_lib, name, &ret));
104104
return ret;
105105
}
106106
static MLC_SYMBOL_HIDE inline MLCTypeTableHandle _lib = []() {
107107
MLCTypeTableHandle ret = nullptr;
108-
MLC_CHECK_ERR(::MLCHandleGetGlobal(&ret), nullptr);
108+
MLC_CHECK_ERR(::MLCHandleGetGlobal(&ret));
109109
return ret;
110110
}();
111111
static MLC_SYMBOL_HIDE inline MLCVTableHandle cxx_str = VTableGetGlobal("__cxx_str__");

include/mlc/base/utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@
7777
} \
7878
MLC_UNREACHABLE()
7979

80-
#define MLC_CHECK_ERR(Call, Ret) \
80+
#define MLC_CHECK_ERR(Call) \
8181
if (int32_t err_code = (Call)) { \
82-
::mlc::base::FuncCallCheckError(err_code, (Ret)); \
82+
::mlc::base::FuncCallCheckError(err_code, nullptr); \
8383
}
8484

8585
namespace mlc {

include/mlc/core/all.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ inline Any Lib::IRPrint(AnyView obj, AnyView printer, AnyView path) {
147147
return ret;
148148
}
149149
inline int32_t Lib::FuncSetGlobal(const char *name, FuncObj *func, bool allow_override) {
150-
MLC_CHECK_ERR(::MLCFuncSetGlobal(_lib, name, Any(func), allow_override), nullptr);
150+
MLC_CHECK_ERR(::MLCFuncSetGlobal(_lib, name, Any(func), allow_override));
151151
return 0;
152152
}
153153
inline FuncObj *Lib::FuncGetGlobal(const char *name, bool allow_missing) {
154154
Any ret;
155-
MLC_CHECK_ERR(::MLCFuncGetGlobal(_lib, name, &ret), &ret);
155+
MLC_CHECK_ERR(::MLCFuncGetGlobal(_lib, name, &ret));
156156
if (!ret.defined() && !allow_missing) {
157157
MLC_THROW(KeyError) << "Missing global function: " << name;
158158
}
@@ -205,12 +205,12 @@ template <typename R, typename... Args> inline R VTable::operator()(Args... args
205205
AnyViewArray<N> stack_args;
206206
Any ret;
207207
stack_args.Fill(std::forward<Args>(args)...);
208-
MLC_CHECK_ERR(::MLCVTableCall(self, N, stack_args.v, &ret), &ret);
208+
MLC_CHECK_ERR(::MLCVTableCall(self, N, stack_args.v, &ret));
209+
return ret;
209210
}
210211
template <typename Obj> inline VTable &VTable::Set(Func func) {
211212
constexpr bool override_mode = false;
212-
int32_t type_index = Obj::_type_index;
213-
MLC_CHECK_ERR(::MLCVTableSetFunc(this->self, type_index, func.get(), override_mode), nullptr);
213+
MLC_CHECK_ERR(::MLCVTableSetFunc(this->self, Obj::_type_index, func.get(), override_mode));
214214
return *this;
215215
}
216216

include/mlc/core/func.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ inline void FuncCall(const void *self, int32_t num_args, const MLCAny *args, MLC
104104
const MLCFunc *func = static_cast<const MLCFunc *>(self);
105105
if (func->call && reinterpret_cast<void *>(func->safe_call) == reinterpret_cast<void *>(FuncObj::SafeCallImpl)) {
106106
func->call(func, num_args, args, ret);
107-
} else {
108-
MLC_CHECK_ERR(func->safe_call(func, num_args, args, ret), ret);
107+
} else if (int32_t err_code = func->safe_call(func, num_args, args, ret)) {
108+
FuncCallCheckError(err_code, ret);
109109
}
110110
}
111111
template <int32_t num_args> inline auto GetGlobalFuncCall(const char *name) {

include/mlc/core/func_details.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,16 @@ template <typename FuncType, typename> MLC_INLINE FuncObj *FuncObj::Allocator::N
190190
inline Ref<FuncObj> FuncObj::FromForeign(void *self, MLCDeleterType deleter, MLCFuncSafeCallType safe_call) {
191191
if (deleter == nullptr) {
192192
return Ref<FuncObj>::New([self, safe_call](int32_t num_args, const MLCAny *args, MLCAny *ret) {
193-
MLC_CHECK_ERR(safe_call(self, num_args, args, ret), ret);
193+
if (int32_t err_code = safe_call(self, num_args, args, ret)) {
194+
::mlc::base::FuncCallCheckError(err_code, ret);
195+
}
194196
});
195197
} else {
196198
return Ref<FuncObj>::New(
197199
[self = std::shared_ptr<void>(self, deleter), safe_call](int32_t num_args, const MLCAny *args, MLCAny *ret) {
198-
MLC_CHECK_ERR(safe_call(self.get(), num_args, args, ret), ret);
200+
if (int32_t err_code = safe_call(self.get(), num_args, args, ret)) {
201+
::mlc::base::FuncCallCheckError(err_code, ret);
202+
}
199203
});
200204
}
201205
}

include/mlc/core/reflection.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,12 @@ struct _Reflect {
100100
reinterpret_cast<MLCFunc *>(func_any_to_ref.v.v_obj), //
101101
kStaticFn});
102102
}
103-
MLC_CHECK_ERR(::MLCTypeRegisterFields(nullptr, this->type_index, this->fields.size(), this->fields.data()),
104-
nullptr);
103+
MLC_CHECK_ERR(::MLCTypeRegisterFields(nullptr, this->type_index, this->fields.size(), this->fields.data()));
105104
MLC_CHECK_ERR(::MLCTypeRegisterStructure(nullptr, this->type_index, static_cast<int32_t>(this->structure_kind),
106105
this->sub_structure_indices.size(), this->sub_structure_indices.data(),
107-
this->sub_structure_kinds.data()),
108-
nullptr);
106+
this->sub_structure_kinds.data()));
109107
for (const MLCTypeMethod &method : this->methods) {
110-
MLC_CHECK_ERR(::MLCTypeAddMethod(nullptr, this->type_index, method), nullptr);
108+
MLC_CHECK_ERR(::MLCTypeAddMethod(nullptr, this->type_index, method));
111109
}
112110
}
113111
return 0;

0 commit comments

Comments
 (0)