Skip to content

Commit fc5c790

Browse files
committed
Support Object Definition in Python
1 parent dd0116c commit fc5c790

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1823
-785
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
MLC_PYTHON_VERSION: "3.9"
1010
MLC_CIBW_WIN_BUILD: "cp39-win_amd64"
1111
MLC_CIBW_MAC_BUILD: "cp39-macosx_arm64"
12-
MLC_CIBW_LINUX_BUILD: "cp313-manylinux_x86_64"
12+
MLC_CIBW_LINUX_BUILD: "cp312-manylinux_x86_64"
1313

1414
jobs:
1515
pre-commit:

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
22

33
project(
44
mlc
5-
VERSION 0.0.4
5+
VERSION 0.0.5
66
DESCRIPTION "PyMLC"
77
LANGUAGES C CXX
88
)

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
PyMLC
2-
=====
1+
MLC-Python
2+
==========
33

4-
## Features
4+
## Installation
55

6+
```bash
7+
pip install -U mlc-python
8+
```
9+
10+
## Features
611

712
TBA
813

@@ -16,12 +21,6 @@ TBA
1621

1722
## Development
1823

19-
### Install from PyPI
20-
21-
```bash
22-
pip install mlc-python
23-
```
24-
2524
### Build from Source
2625

2726
```bash

cpp/c_api.cc

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "./registry.h"
2-
#if defined(__APPLE__)
32
#include <iostream>
4-
#endif
53

64
namespace mlc {
75
namespace registry {
@@ -31,10 +29,9 @@ MLC_API MLCAny MLCGetLastError() {
3129
}
3230

3331
MLC_API int32_t MLCTypeRegister(MLCTypeTableHandle _self, int32_t parent_type_index, const char *type_key,
34-
int32_t type_index, MLCAttrGetterSetter getter, MLCAttrGetterSetter setter,
35-
MLCTypeInfo **out_type_info) {
32+
int32_t type_index, MLCTypeInfo **out_type_info) {
3633
MLC_SAFE_CALL_BEGIN();
37-
*out_type_info = TypeTable::Get(_self)->TypeRegister(parent_type_index, type_index, type_key, getter, setter);
34+
*out_type_info = TypeTable::Get(_self)->TypeRegister(parent_type_index, type_index, type_key);
3835
MLC_SAFE_CALL_END(&last_error);
3936
}
4037

@@ -146,3 +143,33 @@ MLC_API int32_t MLCErrorGetInfo(MLCAny error, int32_t *num_strs, const char ***s
146143
*strs = ret.data();
147144
MLC_SAFE_CALL_END(&last_error);
148145
}
146+
147+
MLC_API void *MLCExtObjCreate(int32_t bytes, int32_t type_index) {
148+
char *data = new char[bytes]();
149+
std::memset(data, 0, bytes);
150+
MLCAny *header = reinterpret_cast<MLCAny *>(data);
151+
header->type_index = type_index;
152+
header->ref_cnt = 0;
153+
header->deleter = MLCExtObjDelete;
154+
return data;
155+
}
156+
157+
MLC_API void MLCExtObjDelete(void *objptr) {
158+
MLCAny *header = reinterpret_cast<MLCAny *>(objptr);
159+
MLCTypeInfo *info = TypeTable::Global()->GetTypeInfo(header->type_index);
160+
if (info == nullptr) { // TODO: error handling
161+
std::cerr << "Cannot find type info for type index: " << header->type_index << std::endl;
162+
std::abort();
163+
}
164+
MLCTypeField *fields = info->fields;
165+
for (int32_t i = 0;; i++) {
166+
MLCTypeField &field = fields[i];
167+
if (field.name == nullptr) {
168+
break;
169+
}
170+
if (field.is_owned_obj_ptr) {
171+
MLCObject *ptr = reinterpret_cast<MLCObjPtr *>(static_cast<char *>(objptr) + field.offset)->ptr;
172+
::mlc::base::DecRef(ptr);
173+
}
174+
}
175+
}

cpp/c_api_tests.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct ReflectionTest : public ObjectRef {
3030
MLC_DEF_OBJ_REF(ReflectionTest, ReflectionTestObj, ObjectRef)
3131
.Field("x_mutable", &ReflectionTestObj::x_mutable)
3232
.FieldReadOnly("y_immutable", &ReflectionTestObj::y_immutable)
33-
.Method("__init__", InitOf<ReflectionTestObj, std::string, int32_t>)
34-
.Method("YPlusOne", &ReflectionTestObj::YPlusOne);
33+
.StaticFn("__init__", InitOf<ReflectionTestObj, std::string, int32_t>)
34+
.MemFn("YPlusOne", &ReflectionTestObj::YPlusOne);
3535
};
3636

3737
/**************** Traceback ****************/
@@ -52,7 +52,7 @@ MLC_REGISTER_FUNC("mlc.testing.throw_exception_from_ffi").set_body([](FuncObj *f
5252
try {
5353
(*func)();
5454
} catch (Exception &error) {
55-
error.MoveToAny(&ret);
55+
ret = std::move(error.data_);
5656
}
5757
return ret;
5858
});

cpp/dso_loader.h

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)