diff --git a/docs/1_5_0.md b/docs/1_5_0.md new file mode 100644 index 00000000..50ad5414 --- /dev/null +++ b/docs/1_5_0.md @@ -0,0 +1,5 @@ +--- +icon: log +title: 'Upgrade to v1.5.0' +order: 25 +--- \ No newline at end of file diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 56f959b1..dac7a3df 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -551,13 +551,14 @@ PyObject* VM::register_user_class(PyObject* mod, StrName name, RegisterFunc _reg mod->attr().set(name, type); _cxx_typeid_map[typeid(T)] = PK_OBJ_GET(Type, type); _register(this, mod, type); - // check if T is trivially constructible - if constexpr(!std::is_default_constructible_v){ - if(!type->attr().contains(__new__)){ + if(!type->attr().contains(__new__)){ + if constexpr(std::is_default_constructible_v) { bind_func(type, __new__, -1, [](VM* vm, ArgsView args){ - vm->NotImplementedError(); - return vm->None; + Type cls_t = PK_OBJ_GET(Type, args[0]); + return vm->heap.gcnew(cls_t); }); + }else{ + bind_func(type, __new__, -1, PK_ACTION(vm->NotImplementedError())); } } return type; diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 63d80d3b..72f968cb 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -27,7 +27,7 @@ PyObject* PyArrayGetItem(VM* vm, PyObject* _0, PyObject* _1){ PK_UNREACHABLE() } -void init_builtins(VM* _vm) { +void __init_builtins(VM* _vm) { #define BIND_NUM_ARITH_OPT(name, op) \ _vm->bind##name(VM::tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \ i64 val; \ @@ -1511,12 +1511,9 @@ void init_builtins(VM* _vm) { } void VM::__post_init_builtin_types(){ - init_builtins(this); - - bind_func(tp_module, __init__, -1, [](VM* vm, ArgsView args) { - vm->NotImplementedError(); - return vm->None; - }); + __init_builtins(this); + + bind_func(tp_module, __new__, -1, PK_ACTION(vm->NotImplementedError())); _all_types[tp_module].m__getattr__ = [](VM* vm, PyObject* obj, StrName name) -> PyObject*{ const Str& path = CAST(Str&, obj->attr(__path__));