diff --git a/src/cffi.h b/src/cffi.h index 60691518..7a95a0c5 100644 --- a/src/cffi.h +++ b/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 -std::enable_if_t, PyObject*> py_var(VM* vm, const T& data){ +std::enable_if_t && !std::is_pointer_v, PyObject*> py_var(VM* vm, const T& data){ return VAR_T(PlainOldData, data); } /*****************************************************************/ +struct NativeProxyFuncCBase { + virtual PyObject* operator()(VM* vm, ArgsView args) = 0; +}; + template -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 +inline void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){ + static_assert(std::is_pod_v); + static_assert(std::is_pointer_v); + 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 \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 15e9250f..05988b67 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -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]);