@@ -78,74 +78,8 @@ struct TypeInfoWrapper {
78
78
~TypeInfoWrapper () { this ->Reset (); }
79
79
};
80
80
81
- template <typename T> struct PODGetterSetter {
82
- static int32_t Getter (MLCTypeField *, void *addr, MLCAny *ret) {
83
- using namespace ::mlc::base;
84
- TypeTraits<T>::TypeToAny (*static_cast <T *>(addr), ret);
85
- return 0 ;
86
- }
87
- static int32_t Setter (MLCTypeField *, void *addr, MLCAny *src) {
88
- using namespace mlc ::base;
89
- try {
90
- *static_cast <T *>(addr) = TypeTraits<T>::AnyToTypeUnowned (src);
91
- } catch (const TemporaryTypeError &) {
92
- std::ostringstream oss;
93
- oss << " Cannot convert from type `" << TypeIndex2TypeKey (src->type_index ) << " ` to `" << TypeTraits<T>::type_str
94
- << " `" ;
95
- *static_cast <::mlc::Any *>(src) = MLC_MAKE_ERROR_HERE (TypeError, oss.str ());
96
- return -2 ;
97
- }
98
- return 0 ;
99
- }
100
- };
101
-
102
- template <> struct PODGetterSetter <std::nullptr_t > {
103
- static int32_t Getter (MLCTypeField *, void *, MLCAny *ret) {
104
- MLC_SAFE_CALL_BEGIN ();
105
- *static_cast <Any *>(ret) = nullptr ;
106
- MLC_SAFE_CALL_END (static_cast <Any *>(ret));
107
- }
108
- static int32_t Setter (MLCTypeField *, void *addr, MLCAny *src) {
109
- MLC_SAFE_CALL_BEGIN ();
110
- *static_cast <void **>(addr) = nullptr ;
111
- MLC_SAFE_CALL_END (static_cast <Any *>(src));
112
- }
113
- };
114
-
115
- MLC_INLINE int32_t ObjPtrGetterDefault (MLCTypeField *, void *addr, MLCAny *ret) {
116
- if (addr == nullptr ) {
117
- ret->type_index = static_cast <int32_t >(MLCTypeIndex::kMLCNone );
118
- ret->v_obj = nullptr ;
119
- } else {
120
- Object *v = static_cast <Object *>(addr);
121
- ret->type_index = v->_mlc_header .type_index ;
122
- ret->v_obj = reinterpret_cast <MLCAny *>(v);
123
- }
124
- return 0 ;
125
- }
126
-
127
- MLC_INLINE int32_t ObjPtrSetterDefault (MLCTypeField *field, void *addr, MLCAny *src) {
128
- if (field->type_annotation == nullptr ) {
129
- std::ostringstream oss;
130
- oss << " Type annotation is required for field `" << field->name << " `" ;
131
- *static_cast <Any *>(src) = MLC_MAKE_ERROR_HERE (InternalError, oss.str ());
132
- return -2 ;
133
- }
134
- int32_t target_type_index = field->type_annotation [0 ]->type_index ;
135
- if (src == nullptr || src->type_index != target_type_index) {
136
- std::ostringstream oss;
137
- oss << " Cannot convert from type `" << ::mlc::base::TypeIndex2TypeKey (src) << " ` to `"
138
- << ::mlc::base::TypeIndex2TypeKey (target_type_index) << " `" ;
139
- *static_cast <Any *>(src) = MLC_MAKE_ERROR_HERE (TypeError, oss.str ());
140
- return -2 ;
141
- }
142
- Ref<Object> *dst = static_cast <Ref<Object> *>(addr);
143
- *dst = reinterpret_cast <Object *>(src->v_obj );
144
- return 0 ;
145
- }
146
-
147
81
struct TypeTable {
148
- using ObjPtr = std::unique_ptr<MLCObject , void (*)(MLCObject *)>;
82
+ using ObjPtr = std::unique_ptr<MLCAny , void (*)(MLCAny *)>;
149
83
150
84
int32_t num_types;
151
85
std::vector<std::unique_ptr<TypeInfoWrapper>> type_table;
@@ -192,7 +126,7 @@ struct TypeTable {
192
126
std::cerr << " Object already exists in the memory pool: " << source;
193
127
std::abort ();
194
128
}
195
- MLCObject *source_casted = reinterpret_cast <MLCObject *>(source);
129
+ MLCAny *source_casted = reinterpret_cast <MLCAny *>(source);
196
130
::mlc::base::IncRef (source_casted);
197
131
it->second = ObjPtr (source_casted, ::mlc::base::DecRef);
198
132
}
@@ -274,8 +208,6 @@ struct TypeTable {
274
208
std::copy (parent->type_ancestors , parent->type_ancestors + parent->type_depth , info->type_ancestors );
275
209
info->type_ancestors [parent->type_depth ] = parent_type_index;
276
210
}
277
- info->getter = ObjPtrSetterDefault;
278
- info->setter = ObjPtrGetterDefault;
279
211
info->fields = nullptr ;
280
212
info->methods = nullptr ;
281
213
wrapper->table = this ;
@@ -329,20 +261,65 @@ struct _POD_REG {
329
261
.MemFn(" __str__" , &::mlc::base::TypeTraits<std::nullptr_t >::__str__);
330
262
inline static const int32_t _int = //
331
263
::mlc::core::ReflectionHelper (static_cast <int32_t >(MLCTypeIndex::kMLCInt ))
264
+ .StaticFn(" __new_ref__" ,
265
+ [](void *_dst, int64_t value) {
266
+ MLCAny **dst = reinterpret_cast <MLCAny **>(_dst);
267
+ MLCAny *ret = ::mlc::PODAllocator<int64_t >::New (value);
268
+ if (*dst != nullptr ) {
269
+ ::mlc::base::DecRef (*dst);
270
+ }
271
+ *dst = ret;
272
+ })
332
273
.MemFn(" __str__" , &::mlc::base::TypeTraits<int64_t >::__str__);
333
274
inline static const int32_t _float = //
334
275
::mlc::core::ReflectionHelper (static_cast <int32_t >(MLCTypeIndex::kMLCFloat ))
276
+ .StaticFn(" __new_ref__" ,
277
+ [](void *_dst, double value) {
278
+ MLCAny **dst = reinterpret_cast <MLCAny **>(_dst);
279
+ MLCAny *ret = ::mlc::PODAllocator<double >::New (value);
280
+ if (*dst != nullptr ) {
281
+ ::mlc::base::DecRef (*dst);
282
+ }
283
+ *dst = ret;
284
+ })
335
285
.MemFn(" __str__" , &::mlc::base::TypeTraits<double >::__str__);
336
286
inline static const int32_t _ptr = //
337
287
::mlc::core::ReflectionHelper (static_cast <int32_t >(MLCTypeIndex::kMLCPtr ))
288
+ .StaticFn(" __new_ref__" ,
289
+ [](void *_dst, void *value) {
290
+ MLCAny **dst = reinterpret_cast <MLCAny **>(_dst);
291
+ MLCAny *ret = ::mlc::PODAllocator<void *>::New (value);
292
+ if (*dst != nullptr ) {
293
+ ::mlc::base::DecRef (*dst);
294
+ }
295
+ *dst = ret;
296
+ })
338
297
.MemFn(" __str__" , &::mlc::base::TypeTraits<void *>::__str__);
339
298
inline static const int32_t _device = //
340
299
::mlc::core::ReflectionHelper (static_cast <int32_t >(MLCTypeIndex::kMLCDevice ))
341
300
.StaticFn(" __init__" , [](AnyView device) { return device.operator DLDevice (); })
301
+ .StaticFn(" __new_ref__" ,
302
+ [](void *_dst, DLDevice value) {
303
+ MLCAny **dst = reinterpret_cast <MLCAny **>(_dst);
304
+ MLCAny *ret = ::mlc::PODAllocator<DLDevice>::New (value);
305
+ if (*dst != nullptr ) {
306
+ ::mlc::base::DecRef (*dst);
307
+ }
308
+ *dst = ret;
309
+ })
342
310
.MemFn(" __str__" , &::mlc::base::TypeTraits<DLDevice>::__str__);
343
311
inline static const int32_t _dtype = //
344
312
::mlc::core::ReflectionHelper (static_cast <int32_t >(MLCTypeIndex::kMLCDataType ))
345
313
.StaticFn(" __init__" , [](AnyView dtype) { return dtype.operator DLDataType (); })
314
+ .StaticFn(" __new_ref__" ,
315
+ [](void *_dst, DLDataType value) {
316
+ MLCAny **dst = reinterpret_cast <MLCAny **>(_dst);
317
+ MLCAny *ret = ::mlc::PODAllocator<DLDataType>::New (value);
318
+ if (*dst != nullptr ) {
319
+ ::mlc::base::DecRef (*dst);
320
+ }
321
+ *dst = ret;
322
+ })
346
323
.MemFn(" __str__" , &::mlc::base::TypeTraits<DLDataType>::__str__);
347
324
inline static const int32_t _str = //
348
325
::mlc::core::ReflectionHelper (static_cast <int32_t >(MLCTypeIndex::kMLCRawStr ))
@@ -358,8 +335,7 @@ inline TypeTable *TypeTable::New() {
358
335
{ \
359
336
using Traits = ::mlc::base::TypeTraits<UnderlyingType>; \
360
337
MLCTypeInfo *info = Self->TypeRegister (-1 , Traits::type_index, Traits::type_str); \
361
- info->setter = PODGetterSetter<UnderlyingType>::Setter; \
362
- info->getter = PODGetterSetter<UnderlyingType>::Getter; \
338
+ (void )info; \
363
339
}
364
340
MLC_TYPE_TABLE_INIT_TYPE (std::nullptr_t , self);
365
341
MLC_TYPE_TABLE_INIT_TYPE (int64_t , self);
@@ -416,12 +392,7 @@ inline void TypeInfoWrapper::SetFields(int64_t new_num_fields, MLCTypeField *fie
416
392
for (int64_t i = 0 ; i < num_fields; i++) {
417
393
dst[i] = fields[i];
418
394
dst[i].name = this ->table ->NewArray (fields[i].name );
419
- int32_t len_type_ann = 0 ;
420
- while (fields[i].type_annotation [len_type_ann] != nullptr ) {
421
- ++len_type_ann;
422
- }
423
- dst[i].type_annotation = reinterpret_cast <MLCTypeInfo **>(this ->table ->NewArray <void *>(len_type_ann + 1 ));
424
- std::copy (fields[i].type_annotation , fields[i].type_annotation + len_type_ann + 1 , dst[i].type_annotation );
395
+ this ->table ->NewObjPtr (&dst[i].ty , dst[i].ty );
425
396
}
426
397
dst[num_fields] = MLCTypeField{};
427
398
std::sort (dst, dst + num_fields, [](const MLCTypeField &a, const MLCTypeField &b) { return a.offset < b.offset ; });
0 commit comments