fix a bug

This commit is contained in:
blueloveTH 2023-04-16 15:16:12 +08:00
parent c01a77d4b4
commit 294877f4e5
4 changed files with 26 additions and 6 deletions

View File

@ -74,4 +74,7 @@ if os.path.exists("plugins/unity/PocketPyUnityPlugin"):
if os.path.exists("plugins/godot/godot-cpp/pocketpy"): if os.path.exists("plugins/godot/godot-cpp/pocketpy"):
shutil.copy("amalgamated/pocketpy.h", "plugins/godot/godot-cpp/pocketpy/src/pocketpy.h") shutil.copy("amalgamated/pocketpy.h", "plugins/godot/godot-cpp/pocketpy/src/pocketpy.h")
if os.path.exists("/mnt/e/PainterEngine/project/pocketpy.h"):
shutil.copy("amalgamated/pocketpy.h", "/mnt/e/PainterEngine/project/pocketpy.h")
print("amalgamated/pocketpy.h") print("amalgamated/pocketpy.h")

View File

@ -105,6 +105,10 @@ struct PyObject{
_attr->~NameDict(); _attr->~NameDict();
pool64.dealloc(_attr); pool64.dealloc(_attr);
} }
void enable_instance_dict(float lf=kInstAttrLoadFactor) noexcept {
_attr = new(pool64.alloc<NameDict>()) NameDict(lf);
}
}; };
template<typename T> template<typename T>

View File

@ -38,6 +38,9 @@ public:
for(PyObject* p : list) _args[i++] = p; for(PyObject* p : list) _args[i++] = p;
} }
// TODO: poor performance
// List is allocated by pool128 while tuple is by pool64
// ...
Tuple(List&& other) noexcept : Tuple(other.size()){ Tuple(List&& other) noexcept : Tuple(other.size()){
for(int i=0; i<_size; i++) _args[i] = other[i]; for(int i=0; i<_size; i++) _args[i] = other[i];
other.clear(); other.clear();

View File

@ -689,7 +689,13 @@ inline PyObject* VM::_py_call(PyObject* callable, ArgsView args, ArgsView kwargs
int i = 0; int i = 0;
if(args.size() < fn.decl->args.size()){ if(args.size() < fn.decl->args.size()){
vm->TypeError(fmt("expected ", fn.decl->args.size(), " positional arguments, but got ", args.size())); vm->TypeError(fmt(
"expected ",
fn.decl->args.size(),
" positional arguments, but got ",
args.size(),
" (", fn.decl->code->name, ')'
));
} }
// prepare args // prepare args
@ -711,7 +717,7 @@ inline PyObject* VM::_py_call(PyObject* callable, ArgsView args, ArgsView kwargs
break; break;
} }
} }
if(i < args.size()) TypeError("too many arguments"); if(i < args.size()) TypeError(fmt("too many arguments", " (", fn.decl->code->name, ')'));
} }
for(int i=0; i<kwargs.size(); i+=2){ for(int i=0; i<kwargs.size(); i+=2){
@ -756,13 +762,17 @@ inline PyObject* VM::call(PyObject* callable, Args args, const Args& kwargs, boo
PyObject* new_f = callable->attr().try_get(__new__); PyObject* new_f = callable->attr().try_get(__new__);
PyObject* obj; PyObject* obj;
if(new_f != nullptr){ if(new_f != nullptr){
obj = call(new_f, std::move(args), kwargs, false); // should not use std::move here, since we will reuse args in possible __init__
obj = call(new_f, args, kwargs, false);
if(!isinstance(obj, OBJ_GET(Type, callable))) return obj;
}else{ }else{
obj = heap.gcnew<DummyInstance>(OBJ_GET(Type, callable), {}); obj = heap.gcnew<DummyInstance>(OBJ_GET(Type, callable), {});
}
PyObject* self; PyObject* self;
PyObject* init_f = get_unbound_method(obj, __init__, &self, false); PyObject* init_f = get_unbound_method(obj, __init__, &self, false);
if (self != _py_null) {
args.extend_self(self); args.extend_self(self);
if (self != _py_null) call(init_f, std::move(args), kwargs, false); call(init_f, std::move(args), kwargs, false);
} }
return obj; return obj;
} }