some update

This commit is contained in:
blueloveTH 2024-05-04 22:35:54 +08:00
parent d4192be0e4
commit e8a76806c7
5 changed files with 28 additions and 20 deletions

View File

@ -3,3 +3,23 @@ icon: log
title: 'Upgrade to v1.5.0' title: 'Upgrade to v1.5.0'
order: 25 order: 25
--- ---
We are applying a major API refactoring in this release. The main goal is to make the API more consistent and easier to use. We are also adding new features and improvements. This release is not backward compatible with the previous versions. Please read the following guide to upgrade your project.
## New style bindings
We introduced the new style bindings `vm->bind` in [`v1.1.3`](https://github.com/pocketpy/pocketpy/releases/tag/v1.1.3) and deprecated the old style bindings `vm->bind_func<>` and `vm->bind_method<>`.
In this release, we added an ARGC-based binding `vm->bind_func` (without template parameter)
to replace the old style bindings. If you are still using `vm->bind_func<>` and `vm->bind_method<>`,
please update your code to use `vm->bind_func` instead.
```cpp
// old style (removed)
vm->bind_func<N>(obj, "add", f_add);
vm->bind_method<M>(obj, "get", f_get);
// new ARGC-based binding
vm->bind_func(obj, "add", N, f_add);
vm->bind_func(obj, "get", M+1, f_get);
```

View File

@ -97,7 +97,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){
F T::*field = lambda_get_userdata<F T::*>(args.begin()); F T::*field = lambda_get_userdata<F T::*>(args.begin());
return VAR(self.*field); return VAR(self.*field);
}; };
PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1, false); PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1);
PK_OBJ_GET(NativeFunc, _0).set_userdata(field); PK_OBJ_GET(NativeFunc, _0).set_userdata(field);
PyObject* _1 = vm->None; PyObject* _1 = vm->None;
if constexpr (!ReadOnly){ if constexpr (!ReadOnly){
@ -107,7 +107,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){
self.*field = py_cast<F>(vm, args[1]); self.*field = py_cast<F>(vm, args[1]);
return vm->None; return vm->None;
}; };
_1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2, false); _1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2);
PK_OBJ_GET(NativeFunc, _1).set_userdata(field); PK_OBJ_GET(NativeFunc, _1).set_userdata(field);
} }
PyObject* prop = VAR(Property(_0, _1)); PyObject* prop = VAR(Property(_0, _1));

View File

@ -160,8 +160,8 @@ struct NativeFunc {
_userdata = data; _userdata = data;
} }
NativeFunc(NativeFuncC f, int argc, bool method); NativeFunc(NativeFuncC f, int argc): f(f), argc(argc) {}
NativeFunc(NativeFuncC f, FuncDecl_ decl); NativeFunc(NativeFuncC f, FuncDecl_ decl): f(f), argc(-1), decl(decl) {}
void check_size(VM* vm, ArgsView args) const; void check_size(VM* vm, ArgsView args) const;
PyObject* call(VM* vm, ArgsView args) const { return f(vm, args); } PyObject* call(VM* vm, ArgsView args) const { return f(vm, args); }

View File

@ -12,18 +12,6 @@ namespace pkpy{
for(auto& decl: func_decls) decl->_gc_mark(); for(auto& decl: func_decls) decl->_gc_mark();
} }
NativeFunc::NativeFunc(NativeFuncC f, int argc, bool method){
this->f = f;
this->argc = argc;
if(argc != -1) this->argc += (int)method;
}
NativeFunc::NativeFunc(NativeFuncC f, FuncDecl_ decl){
this->f = f;
this->argc = -1;
this->decl = decl;
}
struct PySignalObject: PyObject { struct PySignalObject: PyObject {
PySignalObject() : PyObject(Type(0)) { gc_enabled = false; } PySignalObject() : PyObject(Type(0)) { gc_enabled = false; }
void _obj_gc_mark() override {} void _obj_gc_mark() override {}

View File

@ -1223,7 +1223,7 @@ void VM::setattr(PyObject* obj, StrName name, PyObject* value){
} }
PyObject* VM::bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, UserData userdata, BindType bt) { PyObject* VM::bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, UserData userdata, BindType bt) {
PyObject* nf = VAR(NativeFunc(fn, argc, false)); PyObject* nf = VAR(NativeFunc(fn, argc));
PK_OBJ_GET(NativeFunc, nf).set_userdata(userdata); PK_OBJ_GET(NativeFunc, nf).set_userdata(userdata);
switch(bt){ switch(bt){
case BindType::DEFAULT: break; case BindType::DEFAULT: break;
@ -1272,9 +1272,9 @@ PyObject* VM::bind_property(PyObject* obj, const char* name, NativeFuncC fget, N
PK_ASSERT(is_type(obj, tp_type)); PK_ASSERT(is_type(obj, tp_type));
std::string_view name_sv(name); int pos = name_sv.find(':'); std::string_view name_sv(name); int pos = name_sv.find(':');
if(pos > 0) name_sv = name_sv.substr(0, pos); if(pos > 0) name_sv = name_sv.substr(0, pos);
PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1, false); PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1);
PyObject* _1 = vm->None; PyObject* _1 = vm->None;
if(fset != nullptr) _1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2, false); if(fset != nullptr) _1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2);
PyObject* prop = VAR(Property(_0, _1)); PyObject* prop = VAR(Property(_0, _1));
obj->attr().set(StrName(name_sv), prop); obj->attr().set(StrName(name_sv), prop);
return prop; return prop;