@@ -136,10 +136,11 @@ struct ResourcePool {
136
136
137
137
struct TypeTable ;
138
138
139
- struct VTable {
140
- explicit VTable (TypeTable *type_table, std::string name) : type_table(type_table), name(std::move(name)), data() {}
139
+ struct MLCVTable {
140
+ explicit MLCVTable (TypeTable *type_table, std::string name) : type_table(type_table), name(std::move(name)), data() {}
141
141
void Set (int32_t type_index, FuncObj *func, int32_t override_mode);
142
142
FuncObj *GetFunc (int32_t type_index, bool allow_ancestor) const ;
143
+ void Call (int32_t num_args, MLCAny *args, MLCAny *ret) const ;
143
144
144
145
private:
145
146
TypeTable *type_table;
@@ -269,7 +270,7 @@ struct TypeTable {
269
270
std::vector<std::unique_ptr<TypeInfoWrapper>> type_table;
270
271
std::unordered_map<std::string, MLCTypeInfo *> type_key_to_info;
271
272
std::unordered_map<std::string, FuncObj *> global_funcs;
272
- std::unordered_map<std::string, std::unique_ptr<VTable >> global_vtables;
273
+ std::unordered_map<std::string, std::unique_ptr<MLCVTable >> global_vtables;
273
274
std::unordered_map<std::string, std::unique_ptr<DSOLibrary>> dso_libraries;
274
275
std::unordered_map<std::string, DLDataType> dtype_presets{
275
276
{" void" , {kDLOpaqueHandle , 0 , 0 }},
@@ -414,18 +415,18 @@ struct TypeTable {
414
415
415
416
FuncObj *GetVTable (int32_t type_index, const char *attr_key, bool allow_ancestor) {
416
417
if (auto it = this ->global_vtables .find (attr_key); it != this ->global_vtables .end ()) {
417
- VTable *vtable = it->second .get ();
418
+ MLCVTable *vtable = it->second .get ();
418
419
return vtable->GetFunc (type_index, allow_ancestor);
419
420
} else {
420
421
return nullptr ;
421
422
}
422
423
}
423
424
424
- VTable *GetGlobalVTable (const char *name) {
425
+ MLCVTable *GetGlobalVTable (const char *name) {
425
426
if (auto it = this ->global_vtables .find (name); it != this ->global_vtables .end ()) {
426
427
return it->second .get ();
427
428
} else {
428
- std::unique_ptr<VTable > &vtable = this ->global_vtables [name] = std::make_unique<VTable >(this , name);
429
+ std::unique_ptr<MLCVTable > &vtable = this ->global_vtables [name] = std::make_unique<MLCVTable >(this , name);
429
430
return vtable.get ();
430
431
}
431
432
}
@@ -713,7 +714,7 @@ inline TypeTable *TypeTable::New() {
713
714
#undef MLC_TYPE_TABLE_INIT_TYPE_BEGIN
714
715
#undef MLC_TYPE_TABLE_INIT_TYPE_END
715
716
716
- inline void VTable ::Set (int32_t type_index, FuncObj *func, int32_t override_mode) {
717
+ inline void MLCVTable ::Set (int32_t type_index, FuncObj *func, int32_t override_mode) {
717
718
auto [it, success] = this ->data .try_emplace (type_index, nullptr );
718
719
if (!success) {
719
720
if (override_mode == 0 ) {
@@ -740,7 +741,7 @@ inline void VTable::Set(int32_t type_index, FuncObj *func, int32_t override_mode
740
741
this ->type_table ->pool .AddObj (func);
741
742
}
742
743
743
- inline FuncObj *VTable ::GetFunc (int32_t type_index, bool allow_ancestor) const {
744
+ inline FuncObj *MLCVTable ::GetFunc (int32_t type_index, bool allow_ancestor) const {
744
745
if (auto it = this ->data .find (type_index); it != this ->data .end ()) {
745
746
return it->second ;
746
747
}
@@ -757,6 +758,20 @@ inline FuncObj *VTable::GetFunc(int32_t type_index, bool allow_ancestor) const {
757
758
return nullptr ;
758
759
}
759
760
761
+ inline void MLCVTable::Call (int32_t num_args, MLCAny *args, MLCAny *ret) const {
762
+ constexpr bool allow_ancestor = false ;
763
+ if (num_args == 0 ) {
764
+ MLC_THROW (ValueError) << " Calling a vtable requires at least one argument" ;
765
+ }
766
+ int32_t type_index = args[0 ].type_index ;
767
+ FuncObj *func = this ->GetFunc (type_index, allow_ancestor);
768
+ if (func == nullptr ) {
769
+ MLC_THROW (KeyError) << " VTable `" << name
770
+ << " ` doesn't have type registered: " << this ->type_table ->GetTypeInfo (type_index)->type_key ;
771
+ }
772
+ ::mlc::base::FuncCall (func, num_args, args, ret);
773
+ }
774
+
760
775
} // namespace registry
761
776
} // namespace mlc
762
777
0 commit comments