From e8a76806c73235ff0aa005bb88a66985df949bd3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 4 May 2024 22:35:54 +0800 Subject: [PATCH] some update --- docs/1_5_0.md | 22 +++++++++++++++++++++- include/pocketpy/bindings.h | 4 ++-- include/pocketpy/codeobject.h | 4 ++-- src/codeobject.cpp | 12 ------------ src/vm.cpp | 6 +++--- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/docs/1_5_0.md b/docs/1_5_0.md index 50ad5414..0c7c7f31 100644 --- a/docs/1_5_0.md +++ b/docs/1_5_0.md @@ -2,4 +2,24 @@ icon: log title: 'Upgrade to v1.5.0' order: 25 ---- \ No newline at end of file +--- + +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(obj, "add", f_add); +vm->bind_method(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); +``` \ No newline at end of file diff --git a/include/pocketpy/bindings.h b/include/pocketpy/bindings.h index 2f3022e7..e4d76ecd 100644 --- a/include/pocketpy/bindings.h +++ b/include/pocketpy/bindings.h @@ -97,7 +97,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){ F T::*field = lambda_get_userdata(args.begin()); return VAR(self.*field); }; - PyObject* _0 = heap.gcnew(tp_native_func, fget, 1, false); + PyObject* _0 = heap.gcnew(tp_native_func, fget, 1); PK_OBJ_GET(NativeFunc, _0).set_userdata(field); PyObject* _1 = vm->None; if constexpr (!ReadOnly){ @@ -107,7 +107,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){ self.*field = py_cast(vm, args[1]); return vm->None; }; - _1 = heap.gcnew(tp_native_func, fset, 2, false); + _1 = heap.gcnew(tp_native_func, fset, 2); PK_OBJ_GET(NativeFunc, _1).set_userdata(field); } PyObject* prop = VAR(Property(_0, _1)); diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index f9bcc1ff..caf16d55 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -160,8 +160,8 @@ struct NativeFunc { _userdata = data; } - NativeFunc(NativeFuncC f, int argc, bool method); - NativeFunc(NativeFuncC f, FuncDecl_ decl); + NativeFunc(NativeFuncC f, int argc): f(f), argc(argc) {} + NativeFunc(NativeFuncC f, FuncDecl_ decl): f(f), argc(-1), decl(decl) {} void check_size(VM* vm, ArgsView args) const; PyObject* call(VM* vm, ArgsView args) const { return f(vm, args); } diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 528ed366..399cda8b 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -12,18 +12,6 @@ namespace pkpy{ 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 { PySignalObject() : PyObject(Type(0)) { gc_enabled = false; } void _obj_gc_mark() override {} diff --git a/src/vm.cpp b/src/vm.cpp index dfbf10c4..dd9b5490 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -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* nf = VAR(NativeFunc(fn, argc, false)); + PyObject* nf = VAR(NativeFunc(fn, argc)); PK_OBJ_GET(NativeFunc, nf).set_userdata(userdata); switch(bt){ 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)); std::string_view name_sv(name); int pos = name_sv.find(':'); if(pos > 0) name_sv = name_sv.substr(0, pos); - PyObject* _0 = heap.gcnew(tp_native_func, fget, 1, false); + PyObject* _0 = heap.gcnew(tp_native_func, fget, 1); PyObject* _1 = vm->None; - if(fset != nullptr) _1 = heap.gcnew(tp_native_func, fset, 2, false); + if(fset != nullptr) _1 = heap.gcnew(tp_native_func, fset, 2); PyObject* prop = VAR(Property(_0, _1)); obj->attr().set(StrName(name_sv), prop); return prop;