mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
replace UserData
to any
This commit is contained in:
parent
4a1cf10e74
commit
6778d29ffa
@ -80,7 +80,7 @@ int main(){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The 2nd way is to use `vm->bind`'s last parameter `userdata`, you can store a POD type smaller than 8 bytes.
|
The 2nd way is to use `vm->bind`'s last parameter `userdata`, you can store an `pkpy::any` object.
|
||||||
And use `lambda_get_userdata<T>(args.begin())` to get it inside the lambda body.
|
And use `lambda_get_userdata<T>(args.begin())` to get it inside the lambda body.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
@ -66,6 +66,4 @@ bool any_cast(const any& a, T** out){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
using UserData = any;
|
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
@ -130,9 +130,9 @@ struct NativeFunc {
|
|||||||
// new style decl-based call
|
// new style decl-based call
|
||||||
FuncDecl_ decl;
|
FuncDecl_ decl;
|
||||||
|
|
||||||
UserData _userdata;
|
any _userdata;
|
||||||
|
|
||||||
void set_userdata(UserData&& data) {
|
void set_userdata(any&& data) {
|
||||||
if(_userdata){
|
if(_userdata){
|
||||||
throw std::runtime_error("NativeFunc userdata already set");
|
throw std::runtime_error("NativeFunc userdata already set");
|
||||||
}
|
}
|
||||||
@ -186,7 +186,7 @@ struct Py_<NativeFunc> final: PyObject {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
T& lambda_get_userdata(PyObject** p){
|
T& lambda_get_userdata(PyObject** p){
|
||||||
static_assert(std::is_same_v<T, std::decay_t<T>>);
|
static_assert(std::is_same_v<T, std::decay_t<T>>);
|
||||||
UserData* ud;
|
any* ud;
|
||||||
if(p[-1] != PY_NULL) ud = &PK_OBJ_GET(NativeFunc, p[-1])._userdata;
|
if(p[-1] != PY_NULL) ud = &PK_OBJ_GET(NativeFunc, p[-1])._userdata;
|
||||||
else ud = &PK_OBJ_GET(NativeFunc, p[-2])._userdata;
|
else ud = &PK_OBJ_GET(NativeFunc, p[-2])._userdata;
|
||||||
T* out;
|
T* out;
|
||||||
|
@ -306,21 +306,21 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PK_REGION("General Bindings")
|
#if PK_REGION("General Bindings")
|
||||||
PyObject* bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, UserData userdata={}, BindType bt=BindType::DEFAULT);
|
PyObject* bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType::DEFAULT);
|
||||||
PyObject* bind_func(Type type, StrName name, int argc, NativeFuncC fn, UserData userdata={}, BindType bt=BindType::DEFAULT){
|
PyObject* bind_func(Type type, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType::DEFAULT){
|
||||||
return bind_func(_t(type), name, argc, fn, std::move(userdata), bt);
|
return bind_func(_t(type), name, argc, fn, std::move(userdata), bt);
|
||||||
}
|
}
|
||||||
PyObject* bind_property(PyObject*, const char*, NativeFuncC fget, NativeFuncC fset=nullptr);
|
PyObject* bind_property(PyObject*, const char*, NativeFuncC fget, NativeFuncC fset=nullptr);
|
||||||
template<typename T, typename F, bool ReadOnly=false>
|
template<typename T, typename F, bool ReadOnly=false>
|
||||||
PyObject* bind_field(PyObject*, const char*, F T::*);
|
PyObject* bind_field(PyObject*, const char*, F T::*);
|
||||||
|
|
||||||
PyObject* bind(PyObject*, const char*, NativeFuncC, UserData userdata={}, BindType bt=BindType::DEFAULT);
|
PyObject* bind(PyObject*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType::DEFAULT);
|
||||||
template<typename Ret, typename... Params>
|
template<typename Ret, typename... Params>
|
||||||
PyObject* bind(PyObject*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
|
PyObject* bind(PyObject*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
|
||||||
template<typename Ret, typename T, typename... Params>
|
template<typename Ret, typename T, typename... Params>
|
||||||
PyObject* bind(PyObject*, const char*, Ret(T::*)(Params...), BindType bt=BindType::DEFAULT);
|
PyObject* bind(PyObject*, const char*, Ret(T::*)(Params...), BindType bt=BindType::DEFAULT);
|
||||||
|
|
||||||
PyObject* bind(PyObject*, const char*, const char*, NativeFuncC, UserData userdata={}, BindType bt=BindType::DEFAULT);
|
PyObject* bind(PyObject*, const char*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType::DEFAULT);
|
||||||
template<typename Ret, typename... Params>
|
template<typename Ret, typename... Params>
|
||||||
PyObject* bind(PyObject*, const char*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
|
PyObject* bind(PyObject*, const char*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
|
||||||
template<typename Ret, typename T, typename... Params>
|
template<typename Ret, typename T, typename... Params>
|
||||||
|
@ -1227,7 +1227,7 @@ void VM::setattr(PyObject* obj, StrName name, PyObject* value){
|
|||||||
obj->attr().set(name, value);
|
obj->attr().set(name, 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, any userdata, BindType bt) {
|
||||||
PyObject* nf = VAR(NativeFunc(fn, argc));
|
PyObject* nf = VAR(NativeFunc(fn, argc));
|
||||||
PK_OBJ_GET(NativeFunc, nf).set_userdata(std::move(userdata));
|
PK_OBJ_GET(NativeFunc, nf).set_userdata(std::move(userdata));
|
||||||
switch(bt){
|
switch(bt){
|
||||||
@ -1239,11 +1239,11 @@ PyObject* VM::bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, U
|
|||||||
return nf;
|
return nf;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* VM::bind(PyObject* obj, const char* sig, NativeFuncC fn, UserData userdata, BindType bt){
|
PyObject* VM::bind(PyObject* obj, const char* sig, NativeFuncC fn, any userdata, BindType bt){
|
||||||
return bind(obj, sig, nullptr, fn, std::move(userdata), bt);
|
return bind(obj, sig, nullptr, fn, std::move(userdata), bt);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, NativeFuncC fn, UserData userdata, BindType bt){
|
PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, NativeFuncC fn, any userdata, BindType bt){
|
||||||
CodeObject_ co;
|
CodeObject_ co;
|
||||||
try{
|
try{
|
||||||
// fn(a, b, *c, d=1) -> None
|
// fn(a, b, *c, d=1) -> None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user