mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
...
Update cffi.h Update cffi.h Update cffi.h ... ... ...
This commit is contained in:
parent
ca8fd6b7b1
commit
eaaaa6aeb1
24
src/cffi.h
24
src/cffi.h
@ -72,6 +72,7 @@ struct PlainOldData{
|
||||
inline void add_module_c(VM* vm){
|
||||
PyObject* mod = vm->new_module("c");
|
||||
VoidP::register_class(vm, mod);
|
||||
PlainOldData::register_class(vm, mod);
|
||||
}
|
||||
|
||||
inline PyObject* py_var(VM* vm, void* p){
|
||||
@ -97,18 +98,22 @@ T to_plain_old_data(VM* vm, PyObject* var){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_pod_v<T>, PyObject*> py_var(VM* vm, const T& data){
|
||||
std::enable_if_t<std::is_pod_v<T> && !std::is_pointer_v<T>, PyObject*> py_var(VM* vm, const T& data){
|
||||
return VAR_T(PlainOldData, data);
|
||||
}
|
||||
/*****************************************************************/
|
||||
struct NativeProxyFuncCBase {
|
||||
virtual PyObject* operator()(VM* vm, ArgsView args) = 0;
|
||||
};
|
||||
|
||||
template<typename Ret, typename... Params>
|
||||
struct NativeProxyFuncC {
|
||||
struct NativeProxyFuncC final: NativeProxyFuncCBase {
|
||||
static constexpr int N = sizeof...(Params);
|
||||
using _Fp = Ret(*)(Params...);
|
||||
_Fp func;
|
||||
NativeProxyFuncC(_Fp func) : func(func) {}
|
||||
|
||||
PyObject* operator()(VM* vm, ArgsView args) {
|
||||
PyObject* operator()(VM* vm, ArgsView args) override {
|
||||
if (args.size() != N) {
|
||||
vm->TypeError("expected " + std::to_string(N) + " arguments, but got " + std::to_string(args.size()));
|
||||
}
|
||||
@ -128,4 +133,17 @@ struct NativeProxyFuncC {
|
||||
}
|
||||
};
|
||||
|
||||
template<int ARGC, typename T>
|
||||
inline void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){
|
||||
static_assert(std::is_pod_v<T>);
|
||||
static_assert(std::is_pointer_v<T>);
|
||||
static const StrName m_proxy("__proxy__");
|
||||
static const auto wrapper = [](VM* vm, ArgsView args){
|
||||
NativeProxyFuncCBase* pf = CAST(NativeProxyFuncCBase*, args[-2]->attr(m_proxy));
|
||||
return (*pf)(vm, args);
|
||||
};
|
||||
PyObject* func = VAR(NativeFunc(wrapper, ARGC, false));
|
||||
func->attr().set(m_proxy, VAR_T(VoidP, new NativeProxyFuncC(fp)));
|
||||
obj->attr().set(name, func);
|
||||
}
|
||||
} // namespace pkpy
|
@ -468,6 +468,16 @@ inline void init_builtins(VM* _vm) {
|
||||
return VAR(ss.str());
|
||||
});
|
||||
|
||||
_vm->bind_method<0>("str", "to_char_array", [](VM* vm, ArgsView args){
|
||||
const Str& self = _CAST(Str&, args[0]);
|
||||
return VAR(self.c_str_dup());
|
||||
});
|
||||
|
||||
_vm->bind_func<1>("str", "from_char_array", [](VM* vm, ArgsView args){
|
||||
char* p = CAST(char*, args[0]);
|
||||
return VAR(Str(p));
|
||||
});
|
||||
|
||||
/************ PyList ************/
|
||||
_vm->bind_constructor<2>("list", [](VM* vm, ArgsView args) {
|
||||
return vm->asList(args[1]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user