Skip to content

Commit

Permalink
feat(py_class): Feature Complete for V0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
potatomashed committed Dec 14, 2024
1 parent 9c3279b commit cd668fe
Show file tree
Hide file tree
Showing 34 changed files with 1,516 additions and 835 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: mixed-line-ending
Expand Down Expand Up @@ -46,3 +46,9 @@ repos:
# hooks:
# - id: cmake-format
# - id: cmake-lint
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.1.1
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: [feat, fix, ci, chore, test]
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)

project(
mlc
VERSION 0.0.11
VERSION 0.0.12
DESCRIPTION "MLC-Python"
LANGUAGES C CXX
)
Expand Down
42 changes: 30 additions & 12 deletions cpp/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,45 @@ MLC_API int32_t MLCTypeKey2Info(MLCTypeTableHandle _self, const char *type_key,
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCTypeDefReflection(MLCTypeTableHandle self, int32_t type_index, int64_t num_fields,
MLCTypeField *fields, int64_t num_methods, MLCTypeMethod *methods,
int32_t structure_kind, int64_t num_sub_structures, int32_t *sub_structure_indices,
int32_t *sub_structure_kinds) {
MLC_API int32_t MLCTypeRegisterFields(MLCTypeTableHandle _self, int32_t type_index, int64_t num_fields,
MLCTypeField *fields) {
MLC_SAFE_CALL_BEGIN();
auto *type_info = TypeTable::Get(self)->GetTypeInfoWrapper(type_index);
type_info->SetFields(num_fields, fields);
type_info->SetMethods(num_methods, methods);
type_info->SetStructure(structure_kind, num_sub_structures, sub_structure_indices, sub_structure_kinds);
TypeTable::Get(_self)->SetFields(type_index, num_fields, fields);
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCVTableSet(MLCTypeTableHandle _self, int32_t type_index, const char *key, MLCAny *value) {
MLC_API int32_t MLCTypeRegisterStructure(MLCTypeTableHandle _self, int32_t type_index, int32_t structure_kind,
int64_t num_sub_structures, int32_t *sub_structure_indices,
int32_t *sub_structure_kinds) {
MLC_SAFE_CALL_BEGIN();
TypeTable::Get(_self)->SetVTable(type_index, key, static_cast<AnyView *>(value));
TypeTable::Get(_self)->SetStructure(type_index, structure_kind, num_sub_structures, sub_structure_indices,
sub_structure_kinds);
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCVTableGet(MLCTypeTableHandle _self, int32_t type_index, const char *key, MLCAny *value) {
MLC_API int32_t MLCTypeAddMethod(MLCTypeTableHandle _self, int32_t type_index, MLCTypeMethod method) {
MLC_SAFE_CALL_BEGIN();
*static_cast<Any *>(value) = TypeTable::Get(_self)->GetVTable(type_index, key);
TypeTable::Get(_self)->AddMethod(type_index, method);
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCVTableGetGlobal(MLCTypeTableHandle _self, const char *key, MLCVTableHandle *ret) {
MLC_SAFE_CALL_BEGIN();
*ret = TypeTable::Get(_self)->GetGlobalVTable(key);
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCVTableGetFunc(MLCVTableHandle vtable, int32_t type_index, int32_t allow_ancestor, MLCAny *ret) {
using ::mlc::registry::VTable;
MLC_SAFE_CALL_BEGIN();
*static_cast<Any *>(ret) = static_cast<VTable *>(vtable)->GetFunc(type_index, allow_ancestor);
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCVTableSetFunc(MLCVTableHandle vtable, int32_t type_index, MLCFunc *func, int32_t override_mode) {
using ::mlc::registry::VTable;
MLC_SAFE_CALL_BEGIN();
static_cast<VTable *>(vtable)->Set(type_index, static_cast<FuncObj *>(func), override_mode);
MLC_SAFE_CALL_END(&last_error);
}

Expand Down
55 changes: 19 additions & 36 deletions cpp/c_api_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,23 @@ struct TestingCClassObj : public Object {

explicit TestingCClassObj(int8_t i8, int16_t i16, int32_t i32, int64_t i64, float f32, double f64, void *raw_ptr,
DLDataType dtype, DLDevice device, Any any, Func func, UList ulist, UDict udict, Str str_,
Str str_readonly,
//
List<Any> list_any, List<List<int>> list_list_int, Dict<Any, Any> dict_any_any,
Dict<Str, Any> dict_str_any, Dict<Any, Str> dict_any_str,
Dict<Str, List<int>> dict_str_list_int,
//
Optional<int64_t> opt_i64, Optional<double> opt_f64, Optional<void *> opt_raw_ptr,
Optional<DLDataType> opt_dtype, Optional<DLDevice> opt_device, Optional<Func> opt_func,
Optional<UList> opt_ulist, Optional<UDict> opt_udict, Optional<Str> opt_str,
//
Optional<List<Any>> opt_list_any, Optional<List<List<int>>> opt_list_list_int,
Optional<Dict<Any, Any>> opt_dict_any_any, Optional<Dict<Str, Any>> opt_dict_str_any,
Optional<Dict<Any, Str>> opt_dict_any_str,
Str str_readonly, List<Any> list_any, List<List<int>> list_list_int,
Dict<Any, Any> dict_any_any, Dict<Str, Any> dict_str_any, Dict<Any, Str> dict_any_str,
Dict<Str, List<int>> dict_str_list_int, Optional<int64_t> opt_i64, Optional<double> opt_f64,
Optional<void *> opt_raw_ptr, Optional<DLDataType> opt_dtype, Optional<DLDevice> opt_device,
Optional<Func> opt_func, Optional<UList> opt_ulist, Optional<UDict> opt_udict,
Optional<Str> opt_str, Optional<List<Any>> opt_list_any,
Optional<List<List<int>>> opt_list_list_int, Optional<Dict<Any, Any>> opt_dict_any_any,
Optional<Dict<Str, Any>> opt_dict_str_any, Optional<Dict<Any, Str>> opt_dict_any_str,
Optional<Dict<Str, List<int>>> opt_dict_str_list_int)
: i8(i8), i16(i16), i32(i32), i64(i64), f32(f32), f64(f64), raw_ptr(raw_ptr), dtype(dtype), device(device),
any(any), func(func), ulist(ulist), udict(udict), str_(str_), str_readonly(str_readonly),
//
list_any(list_any), list_list_int(list_list_int), dict_any_any(dict_any_any), dict_str_any(dict_str_any),
dict_any_str(dict_any_str), dict_str_list_int(dict_str_list_int),
//
opt_i64(opt_i64), opt_f64(opt_f64), opt_raw_ptr(opt_raw_ptr), opt_dtype(opt_dtype), opt_device(opt_device),
opt_func(opt_func), opt_ulist(opt_ulist), opt_udict(opt_udict), opt_str(opt_str),
//
opt_list_any(opt_list_any), opt_list_list_int(opt_list_list_int), opt_dict_any_any(opt_dict_any_any),
opt_dict_str_any(opt_dict_str_any), opt_dict_any_str(opt_dict_any_str),
opt_dict_str_list_int(opt_dict_str_list_int) {}
any(any), func(func), ulist(ulist), udict(udict), str_(str_), str_readonly(str_readonly), list_any(list_any),
list_list_int(list_list_int), dict_any_any(dict_any_any), dict_str_any(dict_str_any),
dict_any_str(dict_any_str), dict_str_list_int(dict_str_list_int), opt_i64(opt_i64), opt_f64(opt_f64),
opt_raw_ptr(opt_raw_ptr), opt_dtype(opt_dtype), opt_device(opt_device), opt_func(opt_func),
opt_ulist(opt_ulist), opt_udict(opt_udict), opt_str(opt_str), opt_list_any(opt_list_any),
opt_list_list_int(opt_list_list_int), opt_dict_any_any(opt_dict_any_any), opt_dict_str_any(opt_dict_str_any),
opt_dict_any_str(opt_dict_any_str), opt_dict_str_list_int(opt_dict_str_list_int) {}

int64_t i64_plus_one() const { return i64 + 1; }

Expand All @@ -108,14 +99,12 @@ struct TestingCClass : public ObjectRef {
.Field("udict", &TestingCClassObj::udict)
.Field("str_", &TestingCClassObj::str_)
.FieldReadOnly("str_readonly", &TestingCClassObj::str_readonly)
//
.Field("list_any", &TestingCClassObj::list_any)
.Field("list_list_int", &TestingCClassObj::list_list_int)
.Field("dict_any_any", &TestingCClassObj::dict_any_any)
.Field("dict_str_any", &TestingCClassObj::dict_str_any)
.Field("dict_any_str", &TestingCClassObj::dict_any_str)
.Field("dict_str_list_int", &TestingCClassObj::dict_str_list_int)
//
.Field("opt_i64", &TestingCClassObj::opt_i64)
.Field("opt_f64", &TestingCClassObj::opt_f64)
.Field("opt_raw_ptr", &TestingCClassObj::opt_raw_ptr)
Expand All @@ -125,25 +114,19 @@ struct TestingCClass : public ObjectRef {
.Field("opt_ulist", &TestingCClassObj::opt_ulist)
.Field("opt_udict", &TestingCClassObj::opt_udict)
.Field("opt_str", &TestingCClassObj::opt_str)
//
.Field("opt_list_any", &TestingCClassObj::opt_list_any)
.Field("opt_list_list_int", &TestingCClassObj::opt_list_list_int)
.Field("opt_dict_any_any", &TestingCClassObj::opt_dict_any_any)
.Field("opt_dict_str_any", &TestingCClassObj::opt_dict_str_any)
.Field("opt_dict_any_str", &TestingCClassObj::opt_dict_any_str)
.Field("opt_dict_str_list_int", &TestingCClassObj::opt_dict_str_list_int)
//
.MemFn("i64_plus_one", &TestingCClassObj::i64_plus_one)
.StaticFn("__init__",
InitOf<TestingCClassObj, int8_t, int16_t, int32_t, int64_t, float, double, void *, DLDataType, DLDevice,
Any, Func, UList, UDict, Str, Str,
//
List<Any>, List<List<int>>, Dict<Any, Any>, Dict<Str, Any>, Dict<Any, Str>, Dict<Str, List<int>>,
//
Optional<int64_t>, Optional<double>, Optional<void *>, Optional<DLDataType>, Optional<DLDevice>,
Optional<Func>, Optional<UList>, Optional<UDict>, Optional<Str>,
//
Optional<List<Any>>, Optional<List<List<int>>>, Optional<Dict<Any, Any>>,
Any, Func, UList, UDict, Str, Str, List<Any>, List<List<int>>, Dict<Any, Any>, Dict<Str, Any>,
Dict<Any, Str>, Dict<Str, List<int>>, Optional<int64_t>, Optional<double>, Optional<void *>,
Optional<DLDataType>, Optional<DLDevice>, Optional<Func>, Optional<UList>, Optional<UDict>,
Optional<Str>, Optional<List<Any>>, Optional<List<List<int>>>, Optional<Dict<Any, Any>>,
Optional<Dict<Str, Any>>, Optional<Dict<Any, Str>>, Optional<Dict<Str, List<int>>>>);
};

Expand Down
Loading

0 comments on commit cd668fe

Please sign in to comment.