blueloveTH 1 year ago
parent
commit
37c28e0061
3 changed files with 15 additions and 12 deletions
  1. 5 0
      docs/1_5_0.md
  2. 6 5
      include/pocketpy/vm.h
  3. 4 7
      src/pocketpy.cpp

+ 5 - 0
docs/1_5_0.md

@@ -0,0 +1,5 @@
+---
+icon: log
+title: 'Upgrade to v1.5.0'
+order: 25
+---

+ 6 - 5
include/pocketpy/vm.h

@@ -551,13 +551,14 @@ PyObject* VM::register_user_class(PyObject* mod, StrName name, RegisterFunc _reg
     mod->attr().set(name, type);
     mod->attr().set(name, type);
     _cxx_typeid_map[typeid(T)] = PK_OBJ_GET(Type, type);
     _cxx_typeid_map[typeid(T)] = PK_OBJ_GET(Type, type);
     _register(this, mod, type);
     _register(this, mod, type);
-    // check if T is trivially constructible
-    if constexpr(!std::is_default_constructible_v<T>){
-        if(!type->attr().contains(__new__)){
+    if(!type->attr().contains(__new__)){
+        if constexpr(std::is_default_constructible_v<T>) {
             bind_func(type, __new__, -1, [](VM* vm, ArgsView args){
             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<T>(cls_t);
             });
             });
+        }else{
+            bind_func(type, __new__, -1, PK_ACTION(vm->NotImplementedError()));
         }
         }
     }
     }
     return type;
     return type;

+ 4 - 7
src/pocketpy.cpp

@@ -27,7 +27,7 @@ PyObject* PyArrayGetItem(VM* vm, PyObject* _0, PyObject* _1){
     PK_UNREACHABLE()
     PK_UNREACHABLE()
 }
 }
 
 
-void init_builtins(VM* _vm) {
+void __init_builtins(VM* _vm) {
 #define BIND_NUM_ARITH_OPT(name, op)                                                                    \
 #define BIND_NUM_ARITH_OPT(name, op)                                                                    \
     _vm->bind##name(VM::tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) {                              \
     _vm->bind##name(VM::tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) {                              \
         i64 val;                                                                                        \
         i64 val;                                                                                        \
@@ -1511,12 +1511,9 @@ void init_builtins(VM* _vm) {
 }
 }
 
 
 void VM::__post_init_builtin_types(){
 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*{
     _all_types[tp_module].m__getattr__ = [](VM* vm, PyObject* obj, StrName name) -> PyObject*{
         const Str& path = CAST(Str&, obj->attr(__path__));
         const Str& path = CAST(Str&, obj->attr(__path__));